package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.entity.UpdateArticleMessage;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl implements IApLikesBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Value("${topic.like_behavior}")
    private String likeBehavior;
    /**
     * app用户点赞行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeBehavior(LikesBehaviorDto dto) {
        //获取当前用户
        User user = AppThreadLocalUtil.get();
        //用户登录

        //构建用户点赞行为表对象
        ApLikesBehavior like = new ApLikesBehavior();
        BeanUtils.copyProperties(dto,like);
        if (user != null){
            like.setEquipmentId(null);
            like.setUserId(user.getUserId());
        }else {
            like.setEquipmentId(dto.getEquipmentId());
        }
        like.setCreatedTime(new Date());
        //判断该用户的是否有点赞过此篇文章的行为
        Query query = new Query();
        if (user == null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else {
            query.addCriteria(Criteria.where("userId").is(like.getUserId()));
        }
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        int add = 0;
        if (one == null){
            //之前没有过对该文章的点赞行为
            mongoTemplate.save(like);
            add = 1;
        }else {
            if (dto.getOperation() != one.getOperation()){
                if (dto.getOperation() == 0){
                    add = 1;
                }else {
                    add = -1;
                }
                one.setOperation(dto.getOperation());
                mongoTemplate.save(one);
            }
        }

        //发消息给文章微服务,该文章的点赞数+1
        UpdateArticleMessage message = new UpdateArticleMessage();
        message.setArticleId(dto.getArticleId());
        message.setAdd(add);
        message.setType(1);//操作类型 0 阅读 1 点赞 2 评论 3 收藏
        kafkaTemplate.send(likeBehavior, JSON.toJSONString(message));

        return ResponseResult.okResult();
    }

    /**
     * 查询点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<ApLikesBehavior> getLikes(LikesBehaviorDto dto) {
        Query query =new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if (dto.getUserId() == null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else {
            query.addCriteria(Criteria.where("userId").is(dto.getUserId()));
        }
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        return ResponseResult.okResult(one);
    }
}
